﻿Imports System.Text
Imports Global_Visas_Business_Layer
Imports System.ComponentModel
Imports System.Data
Imports System.Reflection
Imports System.Windows.Media
Imports Telerik.Windows.Controls
Imports Telerik.Windows.Controls.GridView

Partial Public Class frmUnassignedCases
    Public Event CloseDynamicTab(ByVal userControlName As String)
    Private _NotSubscribed As Boolean = True
    Private countLoad As Integer = 0
    Private _initialLoadCases As Boolean
    Dim progressBar As frmProgressBar
    Public Property MinimizeLoadCasesLoops() As Boolean
        Get
            Return _initialLoadCases
        End Get
        Set(ByVal value As Boolean)
            _initialLoadCases = value
        End Set
    End Property
    Private Sub LoadConsultants(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpDept.SelectionChanged
        With Me.drpAssignCon

            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList(True, drpDept.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub


    Private Sub LoadDepartment(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpBranch.SelectionChanged


        With Me.drpDept
            .DisplayMemberPath = "Department_Name"
            .SelectedValuePath = "Department_ID"
            .ItemsSource = CType(CommonDataFunctions.DepartmentList(Me.drpBranch.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub
    'Private Sub UnassignedCases_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Loaded

    '    With Me.drpConName
    '        .DisplayMemberPath = "con_name"
    '        .SelectedValuePath = "con_id"
    '        .ItemsSource = CType(CommonDataFunctions.AddExtraRowToDataTable(CommonDataFunctions.ConsultantList(True), 0, "Unassigned"), IListSource).GetList
    '        .SelectedIndex = 0
    '    End With

    '    With Me.drpBranch
    '        .DisplayMemberPath = "Branch_Name"
    '        .SelectedValuePath = "Branch_ID"
    '        .ItemsSource = CType(CommonDataFunctions.BranchList(), IListSource).GetList
    '        .SelectedIndex = 0
    '    End With

    '    With Me.drpBranchCon
    '        .DisplayMemberPath = "Branch_Name"
    '        .SelectedValuePath = "Branch_ID"
    '        .ItemsSource = CType(CommonDataFunctions.BranchList(), IListSource).GetList
    '    End With


    '    Me.LoadConsultants(Nothing, New System.EventArgs)
    '    Dim branchid As Integer = CommonDataFunctions.GetLoggedOnConsultantBranchID

    '    drpBranch.SelectedValue = branchid
    '    drpBranchCon.SelectedValue = branchid

    '    Me.LoadCases(False, 1)
    '    'Me.WireUpEvents()


    'End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        '        Me.Close()
    End Sub

    Private Sub LoadCases(ByVal bIgnoreProducts As Boolean, Optional ByVal Branchid As Integer = 0)
        Dim dt As DataTable = Nothing
        Try
            progressBar = New frmProgressBar()
            progressBar.TextToDisplay = "Please wait while CRM loads the unassigned WebLeads !"
            progressBar.Show()

            Dim ConType As Admin.ConsultantType
            Dim ConStatus As Admin.ConsultantStatus
            Dim ProductTypeID As Integer


            ConStatus = Admin.ConsultantStatus.Unassigned

            'If Me.radViewAccMan.isChecked Then
            'ConType = Admin.ConsultantType.AccountManager
            If radViewCW Is Nothing Or radViewSales Is Nothing Or radCaseCoordinator Is Nothing Or drpProductTypes Is Nothing Or grdCases Is Nothing Or drpConName Is Nothing Or lblRecords Is Nothing Then
                ' MsgBox("Not found")
                Exit Sub
            End If
            If Me.radViewCW.IsChecked Then
                ConType = Admin.ConsultantType.CaseWorker
            ElseIf Me.radViewSales.IsChecked Then
                ConType = Admin.ConsultantType.SalesCon
            ElseIf Me.radCaseCoordinator.IsChecked Then
                ConType = Admin.ConsultantType.CaseCoordinator
            End If

            Dim selectedRow As DataRowView = Me.drpProductTypes.SelectedItem
            If (drpProductTypes.SelectedIndex <> -1) Then
                ProductTypeID = selectedRow.Row(0).ToString()
            End If


            If bIgnoreProducts = False Then
                'drpProductTypes.Items.Clear()
                'If Branchid > 0 Then
                dt = Products.ProductTypesList
                'End If
                If Not dt Is Nothing Then
                    With drpProductTypes
                        .DisplayMemberPath = "Product Type"
                        .SelectedValuePath = "PRODUCTTYPE_ID"
                        .ItemsSource = CType(CommonDataFunctions.AddExtraRowToDataTable(dt, 0, "All Product Types"), IListSource).GetList
                        .SelectedIndex = 0
                    End With
                End If
                ProductTypeID = Me.drpProductTypes.SelectedValue
            End If

            Dim unassignedcases As DataTable = Admin.GetUnassignedCases(ConType, ConStatus, 0, Me.drpConName.SelectedValue, ProductTypeID)

            Me.grdCases.ItemsSource = FilterRowsAndReturnDistinctValues(unassignedcases)
            Me.grdCases.Columns(0).IsVisible = False
            'Me.grdCases.ItemsSource = unassignedCases
            'unassignedCases = Nothing
            'For currentRowConter As Integer = 0 To unassignedCases.Rows.Count - 1
            '    Dim currentRow As DataRow = unassignedCases.Rows(currentRowConter)
            '    Select Case currentRow(0).ToString().Trim()

            '        Case "No Score"
            '            Dim t As Type = GetType(Brushes)
            '            Dim currentBrush As Brush
            '            currentBrush = t.InvokeMember("White", BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
            '            Me.grdCases.Columns(0).Background = currentBrush
            '            Exit Select

            '        Case "Red"
            '            Dim t As Type = GetType(Brushes)
            '            Dim currentBrush As Brush
            '            currentBrush = t.InvokeMember("Red", BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
            '            Me.grdCases.Columns(0).Background = currentBrush
            '            Exit Select
            '        Case "Orange"
            '            Dim t As Type = GetType(Brushes)
            '            Dim currentBrush As Brush
            '            currentBrush = t.InvokeMember("Orange", BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
            '            Me.grdCases.Columns(0).Background = currentBrush
            '            Exit Select
            '        Case "Green"
            '            Dim t As Type = GetType(Brushes)
            '            Dim currentBrush As Brush
            '            currentBrush = t.InvokeMember("Green", BindingFlags.GetProperty, Nothing, Nothing, Nothing, Nothing)
            '            Me.grdCases.Columns(0).Background = currentBrush
            '            Exit Select
            '    End Select
            'Next
            'Me.grdCases.Columns(Me.grdCases.Columns .Count -1).Background =
            Me.grdCases.SelectedItems.Clear()

            If Not IsNothing(Me.grdCases.ItemsSource) Then
                Me.lblRecords.Content = CType(Me.grdCases.ItemsSource, DataTable).Rows.Count
            End If

            'Me.WireUpEvents()

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            progressBar.Close()
            Me.Focus()
            dt = Nothing
        End Try

    End Sub

    Public Function FilterRowsAndReturnDistinctValues(ByVal dt As DataTable) As DataTable
        Dim dtt As DataTable = dt.Clone()
        Dim currentCaseId As String = String.Empty
        Dim nextCaseId As String = String.Empty
        Dim lastCaseIdSuccessfullyCopied As String = String.Empty
        Dim currentCaseIndex As Integer
        Dim nextCaseIndex As Integer
        For index As Integer = 0 To dt.Rows.Count - 1
            currentCaseIndex = index
            If (index < dt.Rows.Count - 1) Then
                nextCaseIndex = index + 1
            Else
                nextCaseIndex = 0
            End If

            If (nextCaseIndex <> 0) Then
                currentCaseId = dt.Rows(index)("Case_ID").ToString()
                nextCaseId = dt.Rows(index + 1)("Case_ID").ToString()
            End If

            If (index = dt.Rows.Count - 1) Then
                currentCaseId = dt.Rows(index)("Case_ID").ToString()
                If (currentCaseId <> lastCaseIdSuccessfullyCopied) Then
                    Dim drr As DataRow = dtt.NewRow()
                    drr.BeginEdit()
                    For columnIndex As Integer = 0 To dtt.Columns.Count - 1
                        drr(columnIndex) = dt.Rows(index)(columnIndex)
                    Next
                    drr.EndEdit()
                    dtt.Rows.Add(drr)
                End If
            End If

            If (currentCaseId <> nextCaseId And currentCaseId <> lastCaseIdSuccessfullyCopied And nextCaseId <> String.Empty) Then
                Dim drr As DataRow = dtt.NewRow()
                drr.BeginEdit()
                For columnIndex As Integer = 0 To dtt.Columns.Count - 1
                    drr(columnIndex) = dt.Rows(index)(columnIndex)
                Next
                drr.EndEdit()
                dtt.Rows.Add(drr)
                lastCaseIdSuccessfullyCopied = currentCaseId
            End If
        Next
        Return dtt
    End Function

    Private Sub ViewChanged(ByVal sender As Object, ByVal e As System.EventArgs) ' Handles drpConName.SelectedIndexChanged, drpDepartment.SelectedIndexChanged, radConActive.CheckedChanged, radConInactive.CheckedChanged, radConUnassigned.CheckedChanged, radViewAccMan.CheckedChanged, radViewCW.CheckedChanged, radViewSales.CheckedChanged

        'MsgBox(CType(sender, RadioButton).Name)

        'Me.UnwireUpEvents()
        'Me.Cursor = Cursors.Wait
        If Me.MinimizeLoadCasesLoops = False Then
            LoadCases(True)
        End If
        'Me.Cursor = Cursors.Arrow
        Me.lvwSelected.ItemsSource = Nothing
        'Me.WireUpEvents()
        ' blnLoadCases = True
    End Sub

    'Private Sub WireUpEvents()

    '    'Do this after form loads has it fires about 10 times
    '    AddHandler drpConName.SelectionChanged, AddressOf ViewChanged
    '    AddHandler drpProductTypes.SelectionChanged, AddressOf ViewChanged
    '    'AddHandler drpBranch.SelectedIndexChanged, AddressOf ViewChanged
    '    'AddHandler radConActive.CheckedChanged, AddressOf ViewChanged
    '    'AddHandler radConInactive.CheckedChanged, AddressOf ViewChanged
    '    'AddHandler radConUnassigned.CheckedChanged, AddressOf ViewChanged
    '    'AddHandler radViewAccMan, AddressOf ViewChanged
    '    AddHandler radViewCW.Checked, AddressOf ViewChanged
    '    AddHandler radViewCW.Unchecked, AddressOf ViewChanged
    '    AddHandler radViewSales.Checked, AddressOf ViewChanged
    '    AddHandler radViewSales.Unchecked, AddressOf ViewChanged
    '    AddHandler radCaseCoordinator.Checked, AddressOf ViewChanged
    '    AddHandler radCaseCoordinator.Unchecked, AddressOf ViewChanged
    '    AddHandler drpProductTypes.SelectionChanged, AddressOf ViewChanged

    '    'AddHandler chkNoProd.CheckedChanged, AddressOf ViewChanged
    '    'AddHandler chkRetired.CheckedChanged, AddressOf ViewChanged

    '    'AddHandler chkNoProd.CheckedChanged, AddressOf chkNoProd_CheckedChanged
    '    'AddHandler chkAllCon.CheckedChanged, AddressOf chkAllCon_CheckedChanged
    '    'AddHandler chkAllDep.CheckedChanged, AddressOf chkAllDep_CheckedChanged
    '    'AddHandler chkRetired.CheckedChanged, AddressOf chkRetired_CheckedChanged

    'End Sub

    'Private Sub UnwireUpEvents()

    '    'Do this after form loads has it fires about 10 times
    '    RemoveHandler drpConName.SelectionChanged, AddressOf ViewChanged
    '    RemoveHandler drpProductTypes.SelectionChanged, AddressOf ViewChanged
    '    RemoveHandler radViewCW.Checked, AddressOf ViewChanged
    '    RemoveHandler radViewCW.Unchecked, AddressOf ViewChanged
    '    RemoveHandler radViewSales.Checked, AddressOf ViewChanged
    '    RemoveHandler radViewSales.Unchecked, AddressOf ViewChanged
    '    RemoveHandler radCaseCoordinator.Checked, AddressOf ViewChanged
    '    RemoveHandler radCaseCoordinator.Unchecked, AddressOf ViewChanged
    '    RemoveHandler drpProductTypes.SelectionChanged, AddressOf ViewChanged


    'End Sub


    Private Sub grdCases_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.grdCases.Columns.Count > 0 Then

            Dim myWidth = Me.grdCases.Width - (80 + 100 + 100)

            If myWidth > 0 Then

                'Me.grdCases.Columns(0).Width = CType(80, System.Windows.GridLength)
                'Me.grdCases.Columns(1).Width = 80
                'Me.grdCases.Columns(2).Width = myWidth * 0.14
                'Me.grdCases.Columns(3).Width = myWidth * 0.14
                'Me.grdCases.Columns(4).Width = myWidth * 0.14
                'Me.grdCases.Columns(5).Width = myWidth * 0.1
                'Me.grdCases.Columns(6).Width = 100
                'Me.grdCases.Columns(7).Width = myWidth * 0.1
                'Me.grdCases.Columns(8).Width = 100
                'Me.grdCases.Columns(9).Width = 100
                'Me.grdCases.Columns(10).Width = 80
            End If
        End If
    End Sub


    Private Sub grdCases_SelectedRowsChanged(ByVal sender As Object, ByVal e As EventArgs) Handles grdCases.SelectionChanged
        If _NotSubscribed Then

            'AddHandler System.Windows.Forms.Application.Idle, AddressOf Application_Idle
            AddHandler grdCases.SelectionChanged, AddressOf Application_Idle

            _NotSubscribed = False

            lblEvent.Text = String.Empty
            If Me.grdCases.SelectedItems.Count > 0 Then

                Me.btnAssign.IsEnabled = True
                Me.btnDelete.IsEnabled = True
            Else
                Me.btnAssign.IsEnabled = False
                Me.btnDelete.IsEnabled = False
            End If
        End If
    End Sub


    Private Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)

        'Stops code firing every time row is selected, only fires when all selected rows have fired

        RemoveHandler grdCases.SelectionChanged, AddressOf Application_Idle
        _NotSubscribed = True
        'System.Diagnostics.Debug.WriteLine(grdCases.SelectedRows.Count.ToString() + " rows selected")
        'MsgBox(grdCases.SelectedRows.Count.ToString)
        Me.SelectedCases()


    End Sub

    Private Sub SelectedCases()

        Dim myTable As New DataTable
        Dim myRow As DataRow
        Dim myGridRow As DataRow
        Dim i As Integer

        With myTable.Columns
            .Add(New DataColumn("Case", System.Type.GetType("System.Int32")))
            .Add(New DataColumn("Candidate", System.Type.GetType("System.String")))
            .Add(New DataColumn("Nationality", System.Type.GetType("System.String")))
            .Add(New DataColumn("Residence", System.Type.GetType("System.String")))
            .Add(New DataColumn("Product", System.Type.GetType("System.String")))
            .Add(New DataColumn("Destination", System.Type.GetType("System.String")))
        End With

        For i = 0 To Me.grdCases.SelectedItems.Count - 1


            'If Me.grdCases.SelectedRows(i).Location = Xceed.Grid.RowLocation.Data Then

            ' For Each myGridRow In Me.grdCases.SelectedRows
            myGridRow = grdCases.SelectedItems(i)
            myRow = myTable.NewRow

            With myRow
                .Item("Case") = myGridRow.Item("Case_ID").ToString
                .Item("Candidate") = myGridRow.Item("Candidate").ToString
                .Item("Nationality") = myGridRow.Item("Nationality").ToString
                .Item("Residence") = myGridRow.Item("Residence").ToString
                .Item("Product") = myGridRow.Item("Product").ToString
                .Item("Destination") = myGridRow.Item("Destination").ToString
            End With

            myTable.Rows.Add(myRow)
            'End If

        Next

        DataBinding.BindListView(myTable, Me.lvwSelected)

    End Sub

    Private Sub lvwSelected_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwSelected.SelectionChanged
        If Me.lvwSelected.SelectedItems.Count > 0 Then
            Me.lblEvent.Text = TasksEvents.GetWebCommentsEvent(Me.lvwSelected.SelectedItem.row.itemarray(0).ToString)
        End If

    End Sub

    Private Sub btnAssign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssign.Click

        Dim myCases() As Long = Nothing
        Dim i As Integer
        Dim myRow As DataRow

        If Me.grdCases.SelectedItems.Count = 0 Then
            CommonFuncs.DisplayMessage("No Cases selected", CommonDialog.CustomDialogIcons.Information, "Error")
            Exit Sub
        End If
        Try
            If Me.drpAssignCon.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("No consultant selected", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.drpAssignCon.Focus()
                Exit Sub
            End If
            Dim casesDetails As New List(Of Cases.CaseInfo)
            Dim consalesId As String

            For Each myRow In Me.grdCases.SelectedItems

                ReDim Preserve myCases(i)
                Dim caseInf As New Cases.CaseInfo()
                caseInf.CaseId = Convert.ToDouble(myRow.Item("Case_ID").ToString)
                caseInf.CandidateId = Convert.ToDouble(myRow.Item("candidateid").ToString)
                caseInf.CaseStatusId = Convert.ToInt32(myRow.Item("CaseStatusID").ToString)
                consalesId = myRow.Item("SaleConID").ToString
                If Not (String.IsNullOrEmpty(consalesId)) Then
                    caseInf.SalesConAssignedId = consalesId
                Else
                    caseInf.SalesConAssignedId = drpAssignCon.SelectedValue
                End If
                casesDetails.Add(caseInf)

                myCases(i) = myRow.Item("Case_ID").ToString
                i += 1
            Next
            Dim successFlag As Boolean = False
            If myCases.Length > 0 Then
                If Me.radViewCW.IsChecked Then
                    If Admin.AssignCaseworker(myCases, Admin.ConsultantType.CaseWorker, Me.drpAssignCon.SelectedValue, Me.chkMoveTasks.IsChecked) Then
                        successFlag = True
                        Me.ViewChanged(Me, New System.EventArgs)
                    End If
                ElseIf Me.radViewSales.IsChecked Then
                    If Admin.AssignCaseworker(myCases, Admin.ConsultantType.SalesCon, Me.drpAssignCon.SelectedValue, Me.chkMoveTasks.IsChecked) Then
                        successFlag = True
                        Me.ViewChanged(Me, New System.EventArgs)
                        Dim taskEvents As New Global_Visas_Business_Layer.TasksEvents()
                        For Each caseId As Long In myCases
                            taskEvents.GblFunc_Add_Event("Make a Call", "Day: 1:1", Global_Visas_Business_Layer.TasksEvents.EventStatus.Task, caseId, TasksEvents.EventTypes.EventCase, False, 0, Me.drpAssignCon.SelectedValue, DateTime.Today, 1)
                        Next
                    End If
                ElseIf Me.radCaseCoordinator.IsChecked Then
                    If Admin.AssignCaseworker(myCases, Admin.ConsultantType.CaseCoordinator, Me.drpAssignCon.SelectedValue, Me.chkMoveTasks.IsChecked) Then
                        successFlag = True
                        Me.ViewChanged(Me, New System.EventArgs)
                    End If
                End If
            End If
            If (successFlag = True) Then
                SendEmailToNewWebLeads(casesDetails)
            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmUnassignedCases.btnAssign_Click")
        Finally
        End Try
    End Sub

    Private Sub SendEmailToNewWebLeads(ByVal casesInfo As List(Of Cases.CaseInfo))
        'Send email when case at new web lead and assigned the consultant to candidate
        For Each caseInfo In casesInfo
            If (caseInfo.CaseStatusId = 1 And caseInfo.SalesConid = 0) Then
                CommonDataFunctions.SendEmailUnassignedInformationToClient(caseInfo)
            End If
        Next
    End Sub


    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'Only do this if this is an unassigned case

        'Dim myCases() As Long
        Dim myRow As DataRow
        Dim Msg As StringBuilder
        'Dim i As Integer
        Dim res As MsgBoxResult
        Dim intCheckEachRow As Integer

        If Me.grdCases.SelectedItems.Count = 0 Then
            Exit Sub
        End If

        If Me.grdCases.SelectedItems.Count > 1 Then
            intCheckEachRow = MsgBox("You have selected multiple cases. Do you want to go mad and delete them all in one go, right now?", MsgBoxStyle.YesNoCancel)
        End If

        If intCheckEachRow = MsgBoxResult.Cancel Then
            Exit Sub
        End If

        For Each myRow In Me.grdCases.SelectedItems

            Msg = New StringBuilder

            If Me.grdCases.SelectedItems.Count = 1 Or intCheckEachRow = MsgBoxResult.No Then
                With Msg
                    .Append("Are you sure you wish to delete this lead: (")
                    .Append(myRow.Item("Case_ID").ToString & ",")
                    '.Append(myRow.Cells(" ").Value & ",")
                    .Append(myRow.Item("Nationality").ToString & ",")
                    .Append(myRow.Item("Product").ToString & ") ?")
                End With

                res = MsgBox(Msg.ToString, MsgBoxStyle.YesNoCancel)

                Select Case res
                    Case MsgBoxResult.Cancel
                        Exit For
                    Case MsgBoxResult.Yes
                        Admin.DeleteLead(myRow.Item("Case_ID").ToString)
                    Case MsgBoxResult.No

                End Select

            ElseIf intCheckEachRow = MsgBoxResult.Yes Then
                Admin.DeleteLead(myRow.Item("Case_ID").VaToStringlue)
            Else
                ' do nothing
            End If

        Next

        If Me.MinimizeLoadCasesLoops = False Then
            LoadCases(True)
        End If



    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        If Me.MinimizeLoadCasesLoops = False Then
            LoadCases(True)
        End If
    End Sub

    Private Sub btnClose_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        RaiseEvent CloseDynamicTab("unassigned")
    End Sub

    Private Sub drpBranchCon_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpBranchCon.SelectionChanged

        With Me.drpConName
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList(True, , , drpBranchCon.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With

    End Sub

    Private Sub drpDept_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpDept.SelectionChanged

    End Sub

    Private Sub lvwSelected_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lvwSelected.SelectionChanged
        If Me.lvwSelected.SelectedItems.Count > 0 Then
            Dim dataRowView As DataRowView = lvwSelected.SelectedItem
            Me.lblEvent.Text = TasksEvents.GetWebCommentsEvent(dataRowView(0).ToString())
        End If
    End Sub

    Private Sub drpConName_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpConName.SelectionChanged
        Me.lvwSelected.ItemsSource = Nothing
        If Me.MinimizeLoadCasesLoops = False Then
            LoadCases(True)
        End If
    End Sub

    Private Sub drpProductTypes_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpProductTypes.SelectionChanged
        If Me.MinimizeLoadCasesLoops = False Then
            LoadCases(True)
        End If
    End Sub

    Private Sub radViewCW_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radViewCW.Checked
        If Me.MinimizeLoadCasesLoops = False Then
            LoadCases(True)
        End If
    End Sub

    Private Sub radViewSales_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radViewSales.Checked
        If Me.MinimizeLoadCasesLoops = False Then
            LoadCases(True)
        End If
    End Sub

    Private Sub radCaseCoordinator_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles radCaseCoordinator.Checked
        If Me.MinimizeLoadCasesLoops = False Then
            LoadCases(True)
        End If
    End Sub

    Private Sub frmUnassignedCases_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Try
            GeneralTasks.ChangeTheme(Me)
            GeneralTasks.EnumerateandFill(Me.LogicalChildren)
            If Me.MinimizeLoadCasesLoops = False Then
                Exit Sub
            End If
            If countLoad > 0 Then
                Exit Sub
            End If
            countLoad = countLoad + 1

            With Me.drpConName
                .DisplayMemberPath = "con_name"
                .SelectedValuePath = "con_id"
                .ItemsSource = CType(CommonDataFunctions.AddExtraRowToDataTable(CommonDataFunctions.ConsultantList(True), 0, "Unassigned"), IListSource).GetList
                .SelectedIndex = 0
            End With

            With Me.drpBranch
                .DisplayMemberPath = "Branch_Name"
                .SelectedValuePath = "Branch_ID"
                .ItemsSource = CType(CommonDataFunctions.BranchList(), IListSource).GetList
                .SelectedIndex = 0
            End With

            With Me.drpBranchCon
                .DisplayMemberPath = "Branch_Name"
                .SelectedValuePath = "Branch_ID"
                .ItemsSource = CType(CommonDataFunctions.BranchList(), IListSource).GetList
                .SelectedIndex = 0
            End With

            With drpProductTypes
                .DisplayMemberPath = "Product Type"
                .SelectedValuePath = "PRODUCTTYPE_ID"
                .ItemsSource = CType(CommonDataFunctions.AddExtraRowToDataTable(Products.ProductTypesList, 0, "All Product Types"), IListSource).GetList
                '.ItemsSource = CType(Products.ProductTypesList, IListSource).GetList
                .SelectedIndex = 0
            End With

            Me.LoadConsultants(Nothing, New System.EventArgs)
            Dim branchid As Integer = CommonDataFunctions.GetLoggedOnConsultantBranchID

            drpBranch.SelectedValue = branchid
            drpBranchCon.SelectedValue = branchid

            Me.LoadCases(False, 1)
            Me.MinimizeLoadCasesLoops = False
            'Me.WireUpEvents()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub grdCases_RowLoaded(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.GridView.RowLoadedEventArgs) Handles grdCases.RowLoaded
        Dim gvscore As Object = grdCases.ChildrenOfType(Of GridViewCell)().Where(Function(c) c.Column.UniqueName = "Score").ToList()
        For Each value As Object In gvscore
            'Select Case value.Value.ToString
            '    Case "Orange"
            '        value.background = Brushes.Orange
            '    Case "Green"
            '        value.background = Brushes.Green
            '    Case "Red"
            '        value.background = Brushes.Red

            'End Select
        Next
    End Sub
End Class
